<?php
namespace Dms\Controller;
use Think\Controller;
use visaMessage\visaMessage;

class MessageController extends Controller
{
	private $userID = false;
	private $userGroupID = false;
	
	public function _initialize()
	{
		$this->userID 		= getIdenty();
		$this->userGroupID 	= getIdentyGroupID();
	}

    public function index()
    {
    	//default
    	$sql = "select count(*) c from message";
    	$count = M()->query($sql);
    	$count = $count[0]['c'];
    	$pageClass = new \Think\Page($count,10);
    	$sql = "select * from message where sendID = {$this->userID} or receiveID = {$this->userID} order by sendTime desc limit {$pageClass->firstRow},{$pageClass->listRows}";
    	$defaultMessage=  M()->query($sql);

    	foreach ($defaultMessage as $key => $value)
    	{
    		$defaultMessage[$key]['title'] 	= subtext($value['title'], 40);
    		$defaultMessage[$key]['sendTime']	= date('Y-m-d H:i:s',$value['sendTime']);
    		 
    		if($value['isRead'] == 0)
    		{
    			$defaultMessage[$key]['isNew'] = true;
    		}
    		else
    		{
    			$defaultMessage[$key]['isNew'] = false;
    		}
    	}
    	$this->defaultMessage = $defaultMessage;
    	
    	//order
    	$sql = "select count(*) c from message_order where sendID = {$this->userID} or receiveID = {$this->userID} or (receiveGroupID = {$this->userGroupID} and receiveID = 0)";
    	$count = M()->query($sql);
    	$count = $count[0]['c'];
    	$pageClass = new \Think\Page($count,10);
    	$sql = "select * from message_order where sendID = {$this->userID} or receiveID = {$this->userID} or (receiveGroupID = {$this->userGroupID} and receiveID = 0) order by sendTime desc,id desc limit {$pageClass->firstRow},{$pageClass->listRows}";
    	$orderMessage=  M()->query($sql);

    	foreach ($orderMessage as $key => $value)
    	{
    		$orderMessage[$key]['title'] 	= subtext($value['title'], 40);
    		$orderMessage[$key]['sendTime']	= date('Y-m-d H:i:s',$value['sendTime']);
    			
    		if(($value['sendID'] == $this->userID && $value['sendTag'] == 1) || ($value['receiveID'] == $this->userID && $value['receiveTag'] == 1) || ($value['receiveID'] == 0 && $value['receiveGroupID'] == $this->userGroupID))
    		{
    			$orderMessage[$key]['isNew'] = true;
    		}
    		else
    		{
    			$orderMessage[$key]['isNew'] = false;
    		}
    	}
    	$this->orderMessage = $orderMessage;
    	
    	//visa
    	$sql = "select count(*) c from message_visa where receiveGroupID = {$this->userGroupID}";
    	$count = M()->query($sql);
    	$count = $count[0]['c'];
    	$pageClass = new \Think\Page($count,10);
    	$sql = "select * from message_visa where receiveGroupID = {$this->userGroupID} order by status asc,sendTime desc,id desc limit {$pageClass->firstRow},{$pageClass->listRows}";
    	$visaMessage=  M()->query($sql);
    	
    	foreach ($visaMessage as $key => $value)
    	{
    		$visaMessage[$key]['title'] 	= subtext($value['title'], 40);
    		$visaMessage[$key]['sendTime']	= date('Y-m-d H:i:s',$value['sendTime']);
    		 
    		if($value['status'] == 0)
    		{
    			$visaMessage[$key]['isNew'] = true;
    		}
    		else
    		{
    			$visaMessage[$key]['isNew'] = false;
    		}
    	}
    	$this->visaMessage = $visaMessage;

    	$this->menuName = '信息中心';
    	$this->display();
    }
    
    public function receive()
    {
        $where 	= array();

        $keyword   	= I('get.keyword','','trim');
        $status		= I('get.status',0);
        
        $this->keyword 	= $keyword;
        $this->status	= $status;
      
        if(empty($keyword) == false)
        {
        	$where['_string']='(title like "%'.$keyword.'%")  OR (content like "%'.$keyword.'%")';
        }

        switch($status)
        {
        	case 1:
        		$where['isRead'] = 0;
        		break;
        	case 2:
        		$where['isRead'] = 1;
        		break;
        }

    	$count = M('message')->where($where)->count();
    	$PageClass = new \Think\Page($count,10);
    	$page = $PageClass->show();
    	$message = M('message')->where($where)->limit($PageClass->firstRow.','.$PageClass->listRows)->order('sendTime desc')->select();

    	foreach ($message as $key => $value)
		{
			$message[$key]['title'] 	= substr($value['title'],0,50);
			$message[$key]['sendName'] 	= M('user')->where(array('id'=>$value['sendID']))->getField('userName');
			$message[$key]['content']	= subtext(strip_tags(htmlspecialchars_decode($value['content'])),50);
			$message[$key]['sendTime']		= date('Y-m-d H:i:s',$value['sendTime']);
			
			if($value['isRead'] == 0)
			{
				$message[$key]['isNew'] = true;
			}
			else 
			{
				$message[$key]['isNew'] = false;
			}
		}

    	$this->list 	= $message;
    	$this->menuName = '收件箱';
    	$this->page    = $page;

    	$this->display();	
    }

    public function send()
    {
    	$uid 	= getIdenty();
        $where 	= array();

        $keyword   	= I('get.keyword','','trim');
        $status		= I('get.status',0);
        
        $this->keyword 	= $keyword;
        $this->status	= $status;
        
        $where['sendID'] 	= $uid;
      
        if(empty($keyword) == false)
        {
        	$where['_string']='(title like "%'.$keyword.'%")  OR (content like "%'.$keyword.'%")';
        }

    	$count = M('message')->where($where)->count();
    	$pageClass = new \Think\Page($count,10);
    	$page = $pageClass->show();
    	
    	$sendTimeArr =  M('message')->distinct(true)->field('sendTime')->where($where)->getField('sendTime',true);
    	$IDArr = array();
    	foreach($sendTimeArr as $key => $value)
    	{
    		$whereTmp = $where;
    		$whereTmp['sendTime'] = $value;
    		$IDArr[] = M('message')->where($whereTmp)->order('id asc')->getField('id');
    	}
    	
    	$where['id'] = array('in',$IDArr);

    	$message =  M('message')->where($where)->limit($pageClass->firstRow.','.$pageClass->listRows)->order('sendTime desc')->select();
    	 
    	foreach ($message as $key => $value)
		{
			$receiveList = explode(';',$value['receiveIDList']);
			$receiveList = M('user')->where(array('id'=>array('in',$receiveList)))->getField('userName',true);
			$receiveName = subtext(implode(',', $receiveList), 10);
			
			$message[$key]['title'] 		= $value['title'];
			$message[$key]['receiveName'] 	= $receiveName;
			$message[$key]['content']		= subtext(strip_tags(htmlspecialchars_decode($value['content'])),50);
			$message[$key]['sendTime']		= date('Y-m-d H:i:s',$value['sendTime']);
		}
		
    	$this->typeArr 	= M('message_type')->select();
    	$this->list 	= $message;
    	$this->menuName = '已发送';
    	$this->page    = $page;

    	$this->display();	
    }

    public function detail()
    {
        $id   = I("get.id");
    	
    	$where = array();
    	$where['_string'] 	= "(sendID = {$this->userID}) OR (receiveID = {$this->userID})";
    	$where['id']		= $id;
    	
    	$message = M('message')->where($where)->find();
    	
    	if(empty($message)==true)
    	{
    		$this->error('错误操作');
    	}

    	if($message['isRead'] == 0)
    	{
    		$data = array();
    		$data['isRead'] = 1;
    		$data['readTime'] = time();
    		M('message')->where(array('id'=>$message['id']))->save($data);
    	}
    	
    	$message['sendName']=M('user')->where(array('id'=>$message['sendID']))->getField('userName');
    	$receiveList = explode(';',$message['receiveIDList']);
    	$receiveList = M('user')->where(array('id'=>array('in',$receiveList)))->getField('userName',true);
    	$receiveName = implode(';', $receiveList);
    	$message['receiveName'] = $receiveName;
    	$message['sendTime'] = date('Y-m-d H:i:s',$message['sendTime']);
    	$message['content']= htmlspecialchars_decode($message['content']);
    	
    	$canReply = false;
    	if($message['receiveID'] == $this->userID)
    	{
    		$canReply = true;
    	}

    	$this->canReply	= $canReply;
    	$this->message	= $message;
    	$this->menuName = '查看消息';
    	 
    	$this->display();
    }
    
    public function sent()
	{
	    if(IS_POST)
		{
			$receiveID 	= I("post.receiveID");
			$title 		= I("post.title");
			$content 	= I("post.content");
			 
			$receiveID = trim($receiveID,';');
			 
			if(empty($receiveID) == true)
			{
				$this->error('请填写收件人');
			}
			 
			if(empty($title) == true)
			{
				$this->error('请填写标题');
			}
			 
			if(empty($content) == true)
			{
				$this->error('请填写内容');
			}
			 
			$receiveIDArr = explode(';',$receiveID);
			 
			//合成收件人列表
			$receiveIDListArr = array();
			foreach ($receiveIDArr as $key => $value)
			{
				$receiveIDTmp = M('user')->where(array('userName'=>$value))->getField('id');
				if(empty($receiveIDTmp) == true)
				{
					$this->error('找不到用户');
				}
				$receiveIDListArr[] = $receiveIDTmp;
			}
			$receiveIDList = implode(';',$receiveIDListArr);

			foreach ($receiveIDListArr as $key => $value)
			{
				$data =array
				(
					'sendID'		=> $this->userID,
					'type'         	=> 1,
					'title'         => $title,
					'content'       => $content,
					'sendTime'		=> time(),
					'receiveID'		=> $value,
					'receiveIDList'	=> $receiveIDList,
				);
				M('message')->add($data);
			}
			 
			$this->success('发送成功!',U(MODULE_NAME. '/Message/send'));
			exit;
		 }

       	$gourpList = M('auth_group')->select();
       	foreach ($gourpList as $key =>$value)
       	{
          
            $gourpListi = M('auth_group_access')->where(array('group_id'=>$value['id']))->select();
           
            foreach ($gourpListi as $key1=>$value1)
            {
               $gourpListi[$key1]['userName']=  M('user')->where(array('id'=>$value1['uid']))->getField('userName');
           	}
            $gourpList[$key]['i'] = $gourpListi;   
       	}

       	$this->gourpList   	= $gourpList;
		$this->menuID   	= '';
		$this->menuName 	= '发送信息';
        $this->display();
    }
    public function reply()
    {
       	$id  = I("get.id");
       
      	$message = M('message')->where(array('id'=>$id))->find();
       
       	if(empty($message)==true)
       	{
       		$this->error('错误操作');
       	}

       	if($message['receiveID'] != $this->userID)
       	{
       		$this->error('错误操作');
       	}

       	if(IS_POST)
       	{
       		$receiveID 	= I("post.receiveID");
       		$title 		= I("post.title");
       		$content 	= I("post.content");
       		
       		$receiveID = trim($receiveID,';');
       		
       		if(empty($receiveID) == true)
       		{
       			$this->error('请填写收件人');
       		}
       		
       		if(empty($title) == true)
       		{
       			$this->error('请填写标题');
       		}
       		
       		if(empty($content) == true)
       		{
       			$this->error('请填写内容');
       		}
       		
	       	$receiveIDArr = explode(';',$receiveID);
	       	
	       	//合成新的收件人列表
	       	$receiveIDListArr = array();
			foreach ($receiveIDArr as $key => $value)
			{
				$receiveIDTmp = M('user')->where(array('userName'=>$value))->getField('id');
				if(empty($receiveIDTmp) == true)
				{
					$this->error('找不到用户');
				}
				$receiveIDListArr[] = $receiveIDTmp;
			}
			$receiveIDList = implode(';',$receiveIDListArr);

	       	
	       	foreach ($receiveIDListArr as $key => $value)
	       	{
	       		$data =array(
	       			'sendID'		=> getIdenty(),
	       			'type'			=> 1,
	       			'title'         => $title,
	       			'content'       => $content,
	       			'sendTime'		=> time(),
	       			'receiveID'		=> $value,
	       			'receiveIDList'	=> $receiveIDList,
	       		);
	       		M('message')->add($data);
	       	}
	       	
	       	$this->success('发送成功!',U(MODULE_NAME. '/Message/send'));
	       	exit;
		}

		$message['sendID']	= M('user')->where(array('id'=>$message['sendID']))->getField('userName');
		$receiveList = explode(';',$message['receiveIDList']);
		$receiveList = M('user')->where(array('id'=>array('in',$receiveList)))->getField('userName',true);
		$receiveName = implode(';', $receiveList);
		$message['receiveName'] = $receiveName;
		$message['content']	= htmlspecialchars_decode(($message['content']));
		$message['sendTime'] = date('Y-m-d H:i:s',$message['sendTime']);
		
		$gourpList = M('auth_group')->select();
		foreach ($gourpList as $key =>$value)
		{
		
			$gourpListi = M('auth_group_access')->where(array('group_id'=>$value['id']))->select();
			 
			foreach ($gourpListi as $key1=>$value1)
			{
				$gourpListi[$key1]['userName']=  M('user')->where(array('id'=>$value1['uid']))->getField('userName');
			}
			$gourpList[$key]['i'] = $gourpListi;
		}
		
		$this->gourpList   	= $gourpList;

		$this->message   	= $message;
        $this->menuID   	= '';
        $this->menuName 	= '回复信息';
        $this->display();
    }
    
	public function orderReceive()
    {
    	$where 	= array();
    
    	$keyword   	= I('get.keyword','','trim');
    	$status		= I('get.status',0); //1未处理 2已处理
    	$type		= I('get.type',0);

    	$this->status	= $status;
    	$this->type		= $type;
    	$this->keyword 	= $keyword;

    	$where['_string'] = "sendID = {$this->userID} or receiveID = {$this->userID} or (receiveGroupID = {$this->userGroupID} and receiveID = 0)";
    
    	if(empty($keyword) == false)
    	{
    		$where['_string']='(title like "%'.$keyword.'%")  OR (content like "%'.$keyword.'%")';
    	}
    	
    	switch($status)
    	{
    		case 1:
    			$where['_string'] = "(sendID = {$this->userID} and sendTag = 1) or (receiveID = {$this->userID} and receiveTag = 1) or (receiveGroupID = {$this->userGroupID} and receiveID = 0) ";
    			break;
    		case 2:
    			$where['_string'] = "(sendID = {$this->userID} and sendTag = 0) or (receiveID = {$this->userID} and receiveTag = 0)";
    			break;
    	}

    	$count = M('message_order')->where($where)->count();
    	$pageClass = new \Think\Page($count,10);
    	$page = $pageClass->show();
    	$message =  M('message_order')->where($where)->limit($pageClass->firstRow.','.$pageClass->listRows)->order('sendTime desc')->select();
    	foreach ($message as $key => $value)
    	{
    		$message[$key]['title'] 	= substr($value['title'],0,50);
    		$message[$key]['sendName'] 	= M('user')->where(array('id'=>$value['sendID']))->getField('userName');
    		$message[$key]['content']	= subtext(strip_tags(htmlspecialchars_decode($value['content'])),50);
    		$message[$key]['sendTime']	= date('Y-m-d H:i:s',$value['sendTime']);
    			
    		if(($value['sendID'] == $this->userID && $value['sendTag'] == 1) || ($value['receiveID'] == $this->userID && $value['receiveTag'] == 1) || ($value['receiveID'] == 0 && $value['receiveGroupID'] == $this->userGroupID))
    		{
    			$message[$key]['isNew'] = true;
    		}
    		else
    		{
    			$message[$key]['isNew'] = false;
    		}
    		
    		$circulationStatus = M('order')->where(array('id'=>$value['orderID']))->getField('circulationStatus');
    		
    		switch($circulationStatus)
    		{
    			case 1:
    				$message[$key]['circulationStatus'] = '跟进中';
    				break;
    			case 2:
    				$message[$key]['circulationStatus'] = '已完成';
    				break;
    			case -1:
    				$message[$key]['circulationStatus'] = '已终止';
    				break;
    			default:
    				$message[$key]['circulationStatus'] = '未知状态';
    				break;
    		}
    	}

    	$this->list 	= $message;
    	$this->menuName = '订单流转';
    	$this->page    = $page;

    	$this->display();
    }
    
    public function orderProcessDeal()
    {
    	$id  = I("get.id");
    	$message = M('message_order')->where(array('id'=>$id))->find();
    	
    	if(empty($message)==true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$content = I("post.content");
    		
    		if(empty($content) == true)
    		{
    			$this->error('请填写内容');
    		}

    		if($message['status'] != 1)
    		{
    			$this->error('该订单已经完成，不需要继续跟单');
    		}
    		
    		$messageData = array();
    		if($message['sendID'] == $this->userID)
    		{
    			$messageData['sendTag'] = 0;
    			$messageData['receiveTag'] = 1;
    		}
    		else 
    		{
    			if($message['receiveID'] == 0)
    			{
    				$messageData['receiveID'] = $this->userID;
    			}
    			$messageData['sendTag'] = 1;
    			$messageData['receiveTag'] = 0;
    		}

    		$messageDetailData =array(
    			'sendID'		=> $this->userID,
    			'orderID'		=> $message['orderID'],
    			'messageID'		=> $message['id'],
    			'content'       => $content,
    			'sendTime'		=> time(),
    		);
    		
    		$messageData['content'] = $content;
    		
    		M('message_order_detail')->add($messageDetailData);
    		
    		M('message_order')->where(array('id'=>$message['id']))->save($messageData);

    		$this->success('回复成功!',U(MODULE_NAME. '/Message/orderReceive'));
    		exit;
    	}

    	$messageDetail = M('message_order_detail')->where(array('messageID'=>$message['id']))->order('id asc')->select();
    	foreach ($messageDetail as $key => $value)
    	{
    		$messageDetail[$key]['sendName'] = M('user')->where(array('id'=>$value['sendID']))->getField('userName');
    		$messageDetail[$key]['sendTime'] = date('Y-m-d H:i:s',$value['sendTime']);
    	}
    	
    	$canWrite 	= false; //是否能回复
    	$canEnd 	= false; //是否能结束

    	if($message['status'] == 1)
    	{
    		$canWrite = true;
			if($message['sendID'] == $this->userID)
			{
				$canEnd = true;
			}
    	}
    	
    	$this->messageDetail 	= $messageDetail;
    	$this->message   		= $message;
    	$this->canWrite   		= $canWrite;
    	$this->canEnd   		= $canEnd;
    	$this->menuID   		= '';
    	$this->menuName 		= '回复信息';
    	
    	$this->display();
    }
    
    public function orderProcessDealEnd()
    {
    	$id  	= I("get.id");
    	$isOver = I("get.isOver");
    	
    	$message = M('message_order')->where(array('id'=>$id))->find();
    	 
    	if(empty($message)==true)
    	{
    		$this->error('错误操作');
    	}
 	
    	if($message['sendID'] != $this->userID || $message['status'] != 1)
    	{
    		$this->error('错误操作');
    	}
    	
    	if($isOver == true)
    	{
    		$circulationStatus = -1;
    	}
    	else 
    	{
    		$circulationStatus = 2;
    	}
    	
    	$data = array();
    	$data['sendTag'] 	= 0;
    	$data['receiveTag'] = 0;
    	$data['status'] 	= 0;
    	
    	M('message_order')->where(array('id'=>$message['id']))->save($data);
    	
    	M('order')->where(array('id'=>$message['orderID']))->save(array('circulationStatus'=>$circulationStatus));
    	
    	$this->success('操作成功，请填写跟单状况',U(MODULE_NAME.'/Order/edit',array('id'=>$message['orderID'])));
    }
    
    public function orderProcessDealStop()
    {
    	$this->error("开发中……");
    	exit;
    	
    	$id  	= I("get.id");
    	$time = I("get.time");
    	 
    	$message = M('message_order')->where(array('id'=>$id,'type'=>2))->find();
    
    	if(empty($message)==true)
    	{
    		$this->error('错误操作');
    	}
    
    	$circulationStatus = M('order')->where(array('id'=>$message['orderID']))->getField('circulationStatus');
    	 
    	if($message['sendID'] != $this->userID || $circulationStatus != 1)
    	{
    		$this->error('错误操作');
    	}

    	$data = array();
    	$data['sendTag'] 	= 0;
    	$data['receiveTag'] = 0;
    	M('message')->where(array('id'=>$message['id']))->save($data);
    	 
    	M('order')->where(array('id'=>$message['orderID']))->save(array('circulationStatus'=>$circulationStatus));
    	 
    	$this->success('操作成功，请填写跟单状况',U(MODULE_NAME.'/Order/edit',array('id'=>$message['orderID'])));
    }
    
    public function visaReceive()
    {
    	$where 	= array();
    
    	$keyword   	= I('get.keyword','','trim');
    	$status		= I('get.status',0); //1未处理 2已处理
    
    	$this->keyword 	= $keyword;
    	$this->status	= $status;

    	$where['_string'] = " sendID = {$this->userID} or receiveGroupID = {$this->userGroupID}";
    
    	if(empty($keyword) == false)
    	{
    		$where['_string']='(title like "%'.$keyword.'%")  OR (content like "%'.$keyword.'%")';
    	}
    
    	switch($status)
    	{
    		case 1:
    			$where['_string'] = "receiveGroupID = {$this->userGroupID} and status = 0";
    			break;
    		case 2:
    			$where['_string'] = "sendID = {$this->userID} and status = 0";
    			break;
    		case 3:
    			$where['_string'] = "status = 1";
    			break;
    	}
    
    	$count = M('message_visa')->where($where)->count();
    	$pageClass = new \Think\Page($count,10);
    	$page = $pageClass->show();
    	$message =  M('message_visa')->where($where)->limit($pageClass->firstRow.','.$pageClass->listRows)->order('sendTime desc')->select();
    	foreach ($message as $key => $value)
    	{
    		$message[$key]['title'] 	= substr($value['title'],0,50);
    		$message[$key]['sendName'] 	= M('user')->where(array('id'=>$value['sendID']))->getField('userName');
    		$message[$key]['content']	= subtext(strip_tags(htmlspecialchars_decode($value['content'])),50);
    		$message[$key]['sendTime']	= date('Y-m-d H:i:s',$value['sendTime']);
    		 
    		if($value['receiveGroupID'] == $this->userGroupID && $value['status'] == 0)
    		{
    			$message[$key]['isNew'] = true;
    		}
    		else
    		{
    			$message[$key]['isNew'] = false;
    		}
    		
    		if($value['status'] == 1)
    		{
    			$message[$key]['status'] = "已完成";
    		}
    		else 
    		{
    			$message[$key]['status'] = "跟进中";
    		}
    		
    		$passportInfo = M('user_passport')->where(array('id'=>$value['passportID']))->find();
    		
    		$message[$key]['customerName'] 		= $passportInfo['username'];
    		$message[$key]['customerMobile'] 	= $passportInfo['mobile'];
    		$message[$key]['expressSingle'] 	= $passportInfo['expresssingle'];
    	}

    	$this->list 	= $message;
    	$this->page    	= $page;
    	$this->menuID   = 49;
    	$this->menuName = '签证流转';
    	$this->display();
    }
    
    //进度页面(插入)
    public function visaProcess()
    {
    	$passportID  = I("get.id"); //护照号ID
    	$message = M('message_visa')->where(array('passportID'=>$passportID))->find();
    
    	if(empty($message) == false)
    	{
    		$this->redirect(MODULE_NAME. '/Message/visaProcessDeal',array('id'=>$message['id']));
    	}
    	
    	$passportInfo = M('user_passport')->where(array('id'=>$passportID))->find();
    	
    	if(empty($passportInfo) == true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$result 		= I("post.result");
    		$receiveGroupID = I("post.receiveGroupID");
    		$content 		= I("post.content");
    		
    		if(empty($result) == true)
    		{
    			$this->error('请勾选错误原因');
    		}
    
    		if(empty($content) == true)
    		{
    			$this->error('请填写内容');
    		}
    		
    		$result = implode('，', $result);
    
    		$insertData 					= array();
    		$insertData['sendID'] 			= $this->userID;
    		$insertData['passportID'] 		= $passportID;
    		$insertData['type'] 			= 1;
    		$insertData['title'] 			= '疑难件流转';
    		$insertData['content'] 			= $content;
    		$insertData['receiveID'] 		= 0;
    		$insertData['receiveGroupID'] 	= $receiveGroupID;
    		$insertData['sendTime'] 		= time();
    		$insertData['status'] 			= 0;
    	   
    		$insertID = M('message_visa')->add($insertData);
    		
    		$data = array(
    			'sendID'		=> $this->userID,
    			'messageID'		=> $insertID,
    			'passportID'	=> $passportID,
    			'content'       => $content,
    			'sendTime'		=> time(),
    		);
    
    		M('message_visa_detail')->add($data);
    		
    		normalToDifficult($passportID,$result);
    
    		$this->success('发送成功!',U(MODULE_NAME. '/Message/visaProcessDeal',array('id'=>$insertID)));
    		exit;
    	}

    	$passportInfo = M('user_passport')->where(array('id'=>$passportID))->find();

    	$this->passportInfo 	= $passportInfo;
    	$this->id 				= $passportID;
    	$this->menuID   		= 49;
    	$this->menuName 		= '流转信息';
    
    	$this->display();
    }
    
    public function visaProcessDeal()
    {
    	$id  = I("get.id");
    	$message = M('message_visa')->where(array('id'=>$id))->find();
    	 
    	if(empty($message)==true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$receiveGroupID = I("post.receiveGroupID");
    		$content 		= I("post.content");
    		$result 		= I("post.result");
    
    		if(empty($result) == true && $message['status'] == 1)
    		{
    			$this->error('请勾选错误原因');
    		}
    		
    		$result = implode(';', $result);
    		
    		if(empty($content) == true)
    		{
    			$this->error('请填写内容');
    		}
    		
    		if(empty($receiveGroupID) == false)
    		{
	    		$save = array();
	    		$save['sendID'] 		= $this->userID;
	    		$save['receiveID'] 		= 0;
	    		$save['receiveGroupID'] = $receiveGroupID;
	    		$save['sendTime'] 		= time();
	    		
	    		M('message_visa')->where(array('id'=>$message['id']))->save($save);
    		}
    		
    		if($message['status'] == 1)
    		{
    			normalToDifficult($message['passportID'],$result);
    		}
    		
    		$data = array(
    			'sendID'		=> $this->userID,
    			'messageID'		=> $message['id'],
    			'passportID'	=> $message['passportID'],
    			'content'       => $content,
    			'sendTime'		=> time(),
    		);
    
    		M('message_visa_detail')->add($data);
    		
    		M('message_visa')->where(array('id'=>$message['id']))->save(array('status'=>0));

    		$this->success('发送成功!',U(MODULE_NAME. '/Message/visaProcessDeal',array('id'=>$message['id'])));
    		exit;
    	}
    
    	$messageDetail = M('message_visa_detail')->where(array('messageID'=>$message['id']))->order('id asc')->select();
    	foreach ($messageDetail as $key => $value)
    	{
    		$messageDetail[$key]['sendName']	= M('user')->where(array('id'=>$value['sendID']))->getField('userName');
    		$messageDetail[$key]['sendTime'] 	= date('Y-m-d H:i:s',$value['sendTime']);
    	}

    	$passportInfo = M('user_passport')->where(array('id'=>$message['passportID']))->find();
    	
    	$canWrite = true;
    	$canEnd = $message['status']==1?false:true;
    	
    	
    	$this->canEnd 			= $canEnd;
    	$this->canWrite 		= $canWrite;
    	$this->passportInfo 	= $passportInfo;
    	$this->messageDetail 	= $messageDetail;
    	$this->message   		= $message;
    	$this->menuID   		= 49;
    	$this->menuName 		= '回复信息';
    	 
    	$this->display();
    }
    
    public function visaProcessDealEnd()
    {
    	$id  	= I("get.id");
    	 
    	$message = M('message_visa')->where(array('id'=>$id))->find();
    
    	if(empty($message) == true)
    	{
    		$this->error('错误操作');
    	}

    	$return = difficultToNormal($message['passportID'],array('处理完毕，已正常送签。'),1);
    	
    	if($return['valid'] == true)
    	{
    		M('message_visa')->where(array('id'=>$message['id']))->save(array('status'=>1));
    		$this->success('操作成功',U(MODULE_NAME.'/Visa/passport'));
    	}
    	else 
    	{
    		$this->error($return['msg']);
    	}

    }
}